Skip to content

Let replaceChildren() replace a document's children#1487

Merged
annevk merged 2 commits into
mainfrom
replacechildren-document-validity
Jul 9, 2026
Merged

Let replaceChildren() replace a document's children#1487
annevk merged 2 commits into
mainfrom
replacechildren-document-validity

Conversation

@annevk

@annevk annevk commented Jul 8, 2026

Copy link
Copy Markdown
Member

replaceChildren() removes all of the parent's children before inserting, but "ensure pre-insert validity" still counted them. So replacing a document's children, e.g. document.replaceChildren(element), threw a "HierarchyRequestError" DOMException even though the result would be a valid document.

Generalize the childToExclude concept (added for replace()) into a list of children to exclude from the validity checks, and have replaceChildren() exclude all of the parent's children. As the child being replaced is now handled uniformly, the isReplace boolean is gone.

Fixes #1045.

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff

replaceChildren() removes all of the parent's children before inserting,
but "ensure pre-insert validity" still counted them. So replacing a
document's children, e.g. document.replaceChildren(element), threw a
"HierarchyRequestError" DOMException even though the result would be a
valid document.

Generalize the childToExclude concept (added for replace()) into a list
of children to exclude from the validity checks, and have
replaceChildren() exclude all of the parent's children. As the child
being replaced is now handled uniformly, the isReplace boolean is gone.

Tests: web-platform-tests/wpt#61154.

Fixes #1045.
@annevk annevk added the agenda+ To be discussed at a triage meeting label Jul 8, 2026
@smaug----

Copy link
Copy Markdown
Contributor

Yeah, the change seems reasonable.

@annevk annevk removed the agenda+ To be discussed at a triage meeting label Jul 9, 2026
@annevk
annevk merged commit ea2a74e into main Jul 9, 2026
2 checks passed
@annevk
annevk deleted the replacechildren-document-validity branch July 9, 2026 12:16
webkit-commit-queue pushed a commit to annevk/WebKit that referenced this pull request Jul 10, 2026
https://bugs.webkit.org/show_bug.cgi?id=318945

Reviewed by Ryosuke Niwa.

This aligns us with an upcoming DOM standard change that makes
replaceChildren() consistent with the other mutation methods:

    whatwg/dom#1487

Tests are updated as per:

   web-platform-tests/wpt#61154

Canonical link: https://commits.webkit.org/316909@main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

replaceChildren can not replace a Document's documentElement

2 participants